package com.koron.order.defect.controller;

import com.koron.bean.base.Response;
import com.koron.bean.system.org.OrgVo;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.common.bean.po.OrderAccept;
import com.koron.order.common.controller.OrderAcceptController;
import com.koron.order.defect.bean.po.DefectStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName DefectAcceptController
 * @description
 * @Author zhouj
 * @Date 2021/10/15 18:44
 */
@Slf4j
@RequestMapping("/defect/orderAccept/")
@RestController
@Api(tags = "缺陷工单执行信息")
public class DefectAcceptController extends OrderAcceptController {

    public EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }

    @Autowired
    DefectController defectController;

    @Override
    @ApiOperation(value = "新增 ")
    @PostMapping("saveAcceptInfo")
    public Response<Integer> saveAcceptInfo(@Validated @RequestBody OrderAccept orderAccept) {
        defectController.verityStatus(orderAccept.getOrderId(), DefectStatus.WAIT_ACCEPT.getStatus());
        Response<Integer> insert = super.saveAcceptInfo(orderAccept);
        if (orderAccept.getIsCommit()) {
            // todo 20230914
            if (orderAccept.getResult().equals(0)){
                boolean flag = StringUtils.equalsAny(orderAccept.getStatus(), DefectStatus.WAIT_DISPATCH.getStatus(), DefectStatus.IN_EXECUTE.getStatus());
                Assert.isTrue(flag,"待审核不通过后跳转节点信息有误！");
                defectController.submitDefect(orderAccept.getOrderId(), orderAccept.getStatus());
            }else {
                defectController.submitDefect(orderAccept.getOrderId(), DefectStatus.WAIT_REPORT.getStatus());
            }
        }

        return insert;
    }

    @ApiOperation(value = "新增分析报告")
    @PostMapping("saveReportInfo")
    public Response<Integer> saveReportInfo(@Validated @RequestBody OrderAccept orderAccept) {
        defectController.verityStatus(orderAccept.getOrderId(), DefectStatus.WAIT_REPORT.getStatus());
        // todo 保存分析报告
        Response<Integer> insert = super.saveAcceptInfo(orderAccept);
        if (orderAccept.getIsCommit()) {
            defectController.submitDefect(orderAccept.getOrderId(), DefectStatus.ARCHIVED.getStatus());
        }

        return insert;
    }

    //校验操作权限  设备管理单位下的人才能操作
    Boolean verityOperation(String deptManage) {
        if (StringUtils.isEmpty(deptManage)) {
            return true;
        }

        List<OrgVo> orgVoList = getCurrUser().getOrgVoList();
        if (CollectionUtils.isEmpty(orgVoList)) {
            return false;
        }
        for (OrgVo orgVo : orgVoList) {
            if (orgVo.getId().equals(deptManage) || orgVo.getLevelPath().contains(deptManage)) {
                return true;
            }
        }
        return false;

    }

}
